/**
 * 主页入口文件
 */
define(function (require, exports, module) {


    var UI = require("js/UI");

    var $homeCnt = $("#home .cnt")
    //<editor-folder desc='单选月份'>
    var $tab = $homeCnt.find(".tab");
    var $mouth = $tab.find(".filter .flt1 a").each(function () {
        var $this = $(this), _txt = $this.text();
        $this.unbind("click.curr").bind("click.curr", function () {


            var order = $order.hasClass("desc") ? "desc" : "asc";

            $dataTime.val("");
            $flt2.removeClass("curr");
            $mouth.removeClass("curr");
            $this.addClass("curr");
            UI.ajax("", "post", {date: _txt, order: order}, function (flag, json) {
            })
            return false
        })
    })
    //</editor-folder>

    //<editor-folder desc='时间选择器'>
    var $flt2 = $tab.find(".filter .flt2"), $dataTime = $flt2.find("input");
    var $dataTimePrev = $dataTime.eq(0), $dataTimeNext = $dataTime.eq(1);

    var resetPrev = function (val) {
        $dataTimePrev.datepicker("destroy");
        UI.datetimepicker($dataTimePrev, {
            maxDate: val,
            onSelect: function (data, inst) {
                resetNext(data)
            }
        })
    }
    var resetNext = function (val) {
        $dataTimeNext.datepicker("destroy");
        UI.datetimepicker($dataTimeNext, {
            minDate: val,
            onSelect: function (data, inst) {
                resetPrev(data)
            }
        })
    }
    UI.datetimepicker($dataTimePrev, {
        onSelect: function (data, inst) {
            resetNext(data)
        }
    });
    UI.datetimepicker($dataTimeNext, {
        onSelect: function (data, inst) {
            resetPrev(data)
        }
    });

    //</editor-folder>


    var $search = $tab.find(".filter .flt2 .btn")
    $search.unbind("click.search").bind("click.search", function () {
        var pre_val = $dataTimePrev.val(), next_val = $dataTimeNext.val();
        if (!pre_val) {
            $dataTimePrev.focus();
            return
        }
        if (!next_val) {
            $dataTimeNext.focus();
            return
        }
        var order = $order.hasClass("desc") ? "desc" : "asc";
        UI.ajax("", "get", {prevDate: pre_val, nextDate: next_val, order: order}, function (flag, json) {
            $mouth.removeClass("curr").eq(0).addClass("curr");
            $flt2.addClass("curr");
        })
        return false
    })

    var $tab = $homeCnt.find(".tab");
    var $order = $tab.find(".th .td:last a").unbind("click.desc").bind("click.desc", function () {
        var $this = $(this), _data = {};
        var order = "desc";
        if ($this.hasClass("desc")) {
            $this.removeClass("desc");
            order = "asc"
        } else {
            $this.addClass("desc")
        }
        _data.order = order;
        if ($flt2.hasClass("curr")) {
            var pre_val = $dataTimePrev.val(), next_val = $dataTimeNext.val();
            _data.prevDate = pre_val;
            _data.nextDate = next_val;
        } else {
            _data.date = $mouth.filter(".curr").text()
        }
        UI.ajax("", "post", _data, function (flag, json) {

        })
    })
})